#!/bin/sh
# GScore 仓库镜像同步工具

set -e

# 默认插件目录
DEFAULT_PLUGINS_DIR="gsuid_core/plugins"

# 解析参数
# 第一个参数：镜像源 (gitcode/cnb)，默认为 gitcode
MIRROR="${1:-gitcode}"

# 插件目录固定为默认值
PLUGINS_DIR="$DEFAULT_PLUGINS_DIR"

# 验证镜像源
case "$MIRROR" in
    gitcode|cnb)
        ;;
    *)
        echo "错误: 镜像源必须是 'gitcode' 或 'cnb'"
        exit 1
        ;;
esac

# 根据镜像源设置目标 URL
if [ "$MIRROR" = "gitcode" ]; then
    CORE_URL="https://gitcode.com/gscore-mirror/gsuid_core"
    PLUGIN_URL_PREFIX="https://gitcode.com/gscore-mirror/"
    MIRROR_NAME="GitCode"
else
    CORE_URL="https://cnb.cool/gscore-mirror/gsuid_core"
    PLUGIN_URL_PREFIX="https://cnb.cool/gscore-mirror/"
    MIRROR_NAME="CNB"
fi

echo "开始替换 git remote url..."
echo "镜像源: $MIRROR_NAME ($MIRROR)"
echo "插件目录: $PLUGINS_DIR"
echo ""

# 插件列表（POSIX sh 兼容的写法）
PLUGINS="
XutheringWavesUID
RoverSign
DNAUID
GenshinUID
StarRailUID
ZZZeroUID
DeltaUID
SayuStock
BlueArchiveUID
VAUID
MajsoulUID
WzryUID
LOLegendsUID
CS2UID
ScoreEcho
TodayEcho
"

TOTAL=0
SUCCESS=0
SKIPPED=0

# 处理 gsuid_core 本体
if [ -d ".git" ]; then
    current_url=$(git remote get-url origin 2>/dev/null || echo "")

    if [ -z "$current_url" ]; then
        echo "  ⊘ 无 origin remote"
        SKIPPED=$((SKIPPED + 1))
    else
        echo "  当前: $current_url"
        echo "  目标: $CORE_URL"

        # 检查是否已是目标镜像地址
        is_target_mirror=0
        case "$current_url" in
            *gitcode.com/gscore-mirror*)
                if [ "$MIRROR" = "gitcode" ]; then
                    is_target_mirror=1
                fi
                ;;
            *cnb.cool/gscore-mirror*)
                if [ "$MIRROR" = "cnb" ]; then
                    is_target_mirror=1
                fi
                ;;
        esac

        if [ $is_target_mirror -eq 1 ]; then
            echo "  ✓ 已是目标镜像地址"
            SUCCESS=$((SUCCESS + 1))
        else
            if git remote set-url origin "$CORE_URL" 2>/dev/null; then
                echo "  ✓ 成功"
                SUCCESS=$((SUCCESS + 1))
            else
                echo "  ✗ 失败"
            fi
        fi
    fi
else
    echo "  ⊘ 非 git 仓库或目录不存在"
    SKIPPED=$((SKIPPED + 1))
fi
echo ""

# 检查并收集已存在的插件
EXISTING_PLUGINS=""
for plugin in $PLUGINS; do
    plugin_path="$PLUGINS_DIR/$plugin"
    if [ -d "$plugin_path" ]; then
        if [ -z "$EXISTING_PLUGINS" ]; then
            EXISTING_PLUGINS="$plugin"
        else
            EXISTING_PLUGINS="$EXISTING_PLUGINS
$plugin"
        fi
    fi
done

if [ -z "$EXISTING_PLUGINS" ]; then
    echo "未找到任何插件目录，请检查路径: $PLUGINS_DIR"
    exit 0
fi

PLUGIN_COUNT=$(echo "$EXISTING_PLUGINS" | wc -l | tr -d ' ')
echo "发现 $PLUGIN_COUNT 个插件目录，开始处理..."
echo ""

# 处理插件
for plugin in $EXISTING_PLUGINS; do
    plugin_path="$PLUGINS_DIR/$plugin"

    echo "处理插件: $plugin"

    if [ ! -d "$plugin_path/.git" ]; then
        echo "  ⊘ 非 git 仓库"
        SKIPPED=$((SKIPPED + 1))
        echo ""
        continue
    fi

    cd "$plugin_path"
    current_url=$(git remote get-url origin 2>/dev/null || echo "")

    if [ -z "$current_url" ]; then
        echo "  ⊘ 无 origin remote"
        SKIPPED=$((SKIPPED + 1))
        cd - >/dev/null 2>&1
        echo ""
        continue
    fi

    target_url="${PLUGIN_URL_PREFIX}${plugin}"
    echo "  当前: $current_url"
    echo "  目标: $target_url"

    # 检查是否已是目标镜像地址
    is_target_mirror=0
    case "$current_url" in
        *gitcode.com/gscore-mirror*)
            if [ "$MIRROR" = "gitcode" ]; then
                is_target_mirror=1
            fi
            ;;
        *cnb.cool/gscore-mirror*)
            if [ "$MIRROR" = "cnb" ]; then
                is_target_mirror=1
            fi
            ;;
    esac

    if [ $is_target_mirror -eq 1 ]; then
        echo "  ✓ 已是目标镜像地址"
        SUCCESS=$((SUCCESS + 1))
        cd - >/dev/null 2>&1
        echo ""
        continue
    else
        if git remote set-url origin "$target_url" 2>/dev/null; then
            echo "  ✓ 成功"
            SUCCESS=$((SUCCESS + 1))
        else
            echo "  ✗ 失败"
        fi
    fi

    cd - >/dev/null 2>&1
    echo ""
done

TOTAL=$((PLUGIN_COUNT + 1))
echo "========================================"
echo "完成！总计: $TOTAL | 成功: $SUCCESS | 跳过: $SKIPPED"
echo "========================================"
